<?php


/* This page is to allow the creation and editing of seasons */

function echoYesNoSelect($name, $selectYes) {
    $no = "";
    $yes = "";
    if ($selectYes) {
        $yes = " selected";
    } else {
        $no = " selected";
    }
    echo<<< EOQ
        	<select name="$name">
        		<option value="0"$no>No</option>
        		<option value="1"$yes>Yes</option>
    		</select>
EOQ;
}

/* Start of Page */
include_once ("includes/init.php");
if (!isCommissioner()) {
    $redirect = "index.php";
    $message = "Permission Denied, redirecting";
    include ("includes/redirect.php");
    return;
}
include_once ("includes/header.php");

echo<<< EOQ
<caption><h1>Season Editor<br/>$season->season</h1></caption>
<tr>
  <td align="left" class="sup">This page allows you to modify a season, create a new one or even delete one entirely. Mouse over the names of the fields for more information about them.
  <br />To change season being edited use the &acute;Change Season&acute; button at the bottom left of the page
  <br />The <a href="season_editor.php">conferences</a> page allows you to add and remove teams from a season, and to add or remove conferences from a season</td>
</tr>
EOQ;

$add = getParam("add");
$delete = getParam("delete");
$update = getParam("update");
if (isset ($add)) {
    $id = $sd->createSeason();
    if ($id) {
        $redirect = "seasons.php?reload=true&season_id=$id";
        $message = "Season Added";
        include ("includes/redirect.php");
        return;
    } else {
        echo<<< EOQ
<tr>
  <td align="center" class="error">Unable to create season</td>
</tr>
<tr>
EOQ;
    }
} else
    if (isset ($delete)) {
        $season->delete();
        session_unset("season");
        $redirect = "index.php?reload=true";
        $message = "Season Deleted";
        include ("includes/redirect.php");
        return;
    } else
        if (isset ($update)) {
            $season->season = stripslashes($_POST["name"]);
            $season->shortName = stripslashes($_POST["shortName"]);
            $season->comments = stripslashes($_POST["comments"]);
            $season->opponentApproval = $_POST["opponentApproval"];
            $season->commissionerApproval = $_POST["commissionerApproval"];
            $season->noTeamEdit = $_POST["noTeamEdit"];
            $season->inactive = $_POST["inactive"];
            $season->commissionerId = $_POST["commissioner_id"];
            $season->image = $_POST["image"];
            $season->imageText = $_POST["imageText"];
            $season->spiralInc = $_POST["spiralInc"];
            $season->spiralStart = $_POST["spiralStart"];
            $rs = $season->update();
            if ($rs) {
                $_SESSION["season"] = $season;
                echo<<< EOQ
<tr>
  <td align="center">Season Updated</td>
</tr>
<tr>
EOQ;
            } else {
                echo<<< EOQ
<tr>
  <td align="center" class="error">Unable to update season</td>
</tr>
<tr>
EOQ;

            }
        }
        
echo<<< EOQ
<tr>
  <td>
    <table border="0" cellPadding="5" cellSpacing="0" width="100%">
    <form name="edit" method="post" action="seasons.php">
      <input type="hidden" name="id" value="$season->id" />
      <tr>
        <th><span title="Full Name of the Season">Name</span></th>
        <td><input type="text" size="32" maxsize="64" name="name" value="$season->season"></td>
      </tr>
      <tr>
        <th><span title="Abbreviation used for the Season">Short Name</span></th>
        <td><input type="text" size="16" maxsize="16" name="shortName" value="$season->shortName"></td>
      </tr>
      <tr>
        <th><span title="Text to appear on the season standings page">Comments</span></th>
        <td><textarea name="comments" rows="5" cols="75">$season->comments</textarea></td>
      </tr>
      <tr>
        <th><span title="Does this season require opponent's to approve match submissions">Opponent Approval</span></th>
        <td>
EOQ;
echoYesNoSelect("opponentApproval", $season->opponentApproval);
echo<<< EOQ
		</td>
      </tr>
      <tr>
        <th><span title="Does this season require the commissioner to approve match submissions">Commissioners Approval</span></th>
        <td>
EOQ;
echoYesNoSelect("commissionerApproval", $season->commissionerApproval);
echo<<< EOQ
		</td>
      </tr>
      <tr>
        <th><span title="Are coaches not allowed to edit teams after their first game">No Team Edit</span></th>
        <td>
EOQ;
echoYesNoSelect("noTeamEdit", $season->noTeamEdit);
echo<<< EOQ
		</td>
      </tr>
      <tr>
        <th><span title="Conferences in this season">Conferences</span></th>
EOQ;
if ($season->hasConferences) {
    $first = 1;
    echo "<td>";
    foreach ($season->conferences as $conf) {
        if ($first) {
            $first = false;
        } else {
            echo "<br />";
        }
        echo $conf;
    }
    echo "</td>";

} else {
    echo "<td>None</td>";
}
$image = $season->getImage();
echo<<< EOQ
      </tr>
      <tr>
        <th><span title="Image to use for season">Image</span></th>
        <td><input type="text" size="32" maxsize="128" name="image" value="$season->image"> $image</td>
      </tr>
      <tr>
        <th><span title="Additional text for the image">Image Text</span></th>
        <td><input type="text" size="32" maxsize="255" name="imageText" value="$season->imageText"></td>
      </tr>
      <tr>
        <th><span title="The TV value (in 000s) at which spiralling expenses begin. The default is 1750.">Start Spiralling</span></th>
        <td><input type="text" size="32" maxsize="255" name="spiralStart" value="$season->spiralStart"></td>
      </tr>
      <tr>
        <th><span title="Increment (in 000s) that spiralling expenses go up. The default is 150.">Spiralling Increment</span></th>
        <td><input type="text" size="32" maxsize="255" name="spiralInc" value="$season->spiralInc"></td>
      </tr>
      <tr>
        <th><span title="Is this season still in progress">Inactive</span></th>
        <td>
EOQ;
echoYesNoSelect("inactive", $season->inactive);
		$cd= new CoachDao();
		$admins = $cd->loadAdmins();
echo<<< EOQ
      </tr>
      <tr>
        <th><span title="Person to be notified of match approvals">Commissioner</span></th>
        <td><select name="commissioner_id">
EOQ;
foreach ($admins as $admin) {
	$selected = "";
	if ($admin->id == $season->commissionerId) {
	    $selected = "selected";
	}
    echo<<< EOQ
    	<option value="$admin->id" $selected>$admin->name</option>
EOQ;
}
    echo<<< EOQ
		</select>
EOQ;
if ($season->winner) {
    echo<<< EOQ
		</td>
      </tr>
      <tr>
      	<th><span title="Automatically set to the winner of the final">Winner</span></th>
      	<td>$season->winner</td>
      </tr>
      <tr>
      	<th><span title="Automatically set to the loser of the final">Runner Up</span></th>
      	<td>$season->runnerUp
EOQ;
}
echo<<< EOQ
      	</td>
      </tr>
      <tr>
        <th><span title="Rule Sets allowed in this season">Supported Rule Sets</span></th>
EOQ;
if ($season->restrictedRuleSets()) {
    $first = 1;
    echo "<td>";
    foreach ($season->getAllowedRuleSetIds() as $ruleSetId) {
        if ($first) {
            $first = false;
        } else {
            echo "<br />";
        }
        echo $ruleSets[$ruleSetId]->getShortName();
    }
    echo "</td>";

} else {
    echo "<td>All Rule Sets</td>";
}
echo<<< EOQ
      </tr>
      <tr>
        <th colspan="2"><input type="submit" name="update" value="Update Season" title="Apply changes to this season"></th>
      </tr>
    </form>
    <form name="add" method="post" action="seasons.php">
      <tr>
        <th colspan="2" align="center">
        	<table width="100%">
        		<tr align="center">
        			<td align="center"><input type="submit" name="add" value="Create New Season" title="Create a new season in this league"></td>
			        <td align="center"><input type="submit" name="delete" value="Delete Season" title="Permanently delete this season"></td>
		        </tr>
	        </table>
        </th>
      </tr>
    </form>
    </table>
  </td>
</tr>
EOQ;

include_once ("includes/footer.php");
?>